<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>:enabled and :disabled demo — shipping form</title>
    <link href="https://fonts.googleapis.com/css?family=Josefin+Sans&display=swap" rel="stylesheet">
    <style>
      body {
        font-family: 'Josefin Sans', sans-serif;
        margin: 20px auto;
        max-width: 460px;
      }

      fieldset {
        padding: 10px 30px 0;
        margin-bottom: 20px;
      }

      legend {
        color: white;
        background: black;
        padding: 5px 10px;
      }

      fieldset > div {
        margin-bottom: 20px;
        display: flex;
      }

      button, label, input[type="text"] {
        display: block;
        font-family: inherit;
        font-size: 100%;
        padding: 0;
        margin: 0;
        box-sizing: border-box;
        width: 100%;
        padding: 5px;
        height: 30px;
      }

      input {
        box-shadow: inset 1px 1px 3px #ccc;
        border-radius: 5px;
      }

      input:hover, input:focus {
        background-color: #eee;
      }

      input[type="text"]:disabled {
          background: #eee;
          border: 1px solid #ccc;
      }

      label:has(+ :disabled) {
        color: #aaa;
      }

      button {
        width: 60%;
        margin: 0 auto;
      }
    </style>
  </head>

<body>
    <form>
      <fieldset id="shipping">
        <legend>Shipping address</legend>
        <div>
          <label for="name1">Name: </label>
          <input id="name1" name="name1" type="text" required>
        </div>
        <div>
          <label for="address1">Address: </label>
          <input id="address1" name="address1" type="text" required>
        </div>
        <div>
          <label for="pcode1">Zip/postal code: </label>
          <input id="pcode1" name="pcode1" type="text" required>
        </div>
      </fieldset>
      <fieldset id="billing">
        <legend>Billing address</legend>
        <div>
          <label for="billing-checkbox">Same as shipping address:</label>
          <input type="checkbox" id="billing-checkbox" checked>
        </div>
        <div>
          <label for="name" class="billing-label">Name: </label>
          <input id="name" name="name" type="text" disabled required>
        </div>
        <div>
          <label for="address2" class="billing-label">Address: </label>
          <input id="address2" name="address2" type="text" disabled required>
        </div>
        <div>
          <label for="pcode2" class="billing-label">Zip/postal code: </label>
          <input id="pcode2" name="pcode2" type="text" disabled required>
        </div>
      </fieldset>

      <div><button>Submit</button></div>
    </form>

    <script>
      // Wait for the page to finish loading
      document.addEventListener('DOMContentLoaded', function () {

        // Attach `change` event listener to checkbox
        document.getElementById('billing-checkbox').addEventListener('change', toggleBilling);
      }, false);

      function toggleBilling() {
        // Select the billing text fields
        let billingItems = document.querySelectorAll('#billing input[type="text"]');

        // Toggle the billing text fields
        for (let i = 0; i < billingItems.length; i++) {
          billingItems[i].disabled = !billingItems[i].disabled;
        }
      }
    </script>
</body>

</html>
